/*************************************************************************
	> File Name: sigaction_simple.c
	> Author: Angel Criss
	> Blog: blog.abbycin.tk 
	> Created Time: Tue 17 Jun 2014 07:17:52 PM CST
 ************************************************************************/
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
//struct sigaction act;//, oldact;
void dealing(int sig)
{
    printf("\e[36m creat a loop here for the blocked signal\e[0m\n");
    printf("\e[34m This will block SIGINT\e[0m\n");
    int j; /*若在子程序执行时收到 SIGINT 信号,怎进程将在子程序返回时被 SIGINT 杀死 */
    for(j=0;j<10;j++){

        printf("\n\n\e[33m the j is :\e[0m\t%d\n\n",j);
        sleep(3);
    }
    printf("\n\n now back to the main function\n\n");
}

int main(int argc, char *argv[])
{
    struct sigaction act;
    sigset_t set;
    int i;
    printf("the PID is:\e[35m %ld\e[0m\n",(long)getpid());
    if(sigemptyset(&set)==-1)
    {
        perror("sigfillset:");
        raise(2);
    }
    if(sigaddset(&set,SIGINT)==-1)
    {
        perror("sigaddset:");
        raise(2);
    }
    printf("\e[34m This will handle SIGTERM to block SIGINT\e[0m\n");
    act.sa_handler=dealing;
    act.sa_mask=set;
    act.sa_flags=SA_RESTART;
    if(sigaction(SIGTERM,&act,NULL)==-1)
    {
        perror("sigaction:");
        raise(2);
    }
    printf("\e[32m proccess sleep here for 30s\e[0m\n");
    for(i=0;;i++){
        printf("\n\n the i is :\t%d\n\n",i);
        sleep(3);
    }
}
